import {
  FileManager
} from '../file_manager/FileManager';
import {
  logger
} from '../01-LogManager/Logger_A/log';


/**
 * 开关配置的必要性:
 * 1,有的流程正常流程很难测试到,需要配置进行直接测试.
 */

export interface DebugConfig {
  //自动填充账号,需要在 rawFile中增加配置文件DebugAccount.json才能起效.
  autoFillAccount: boolean
  testPictureVerify: boolean,
  forceShowMaintainence: boolean
  enableSplashTimer: boolean
}

/**
 * 正式环境的配置,不要修改.
 */
const releaseConfig: DebugConfig = {
  autoFillAccount: false,
  testPictureVerify: false,
  forceShowMaintainence: false,
  enableSplashTimer: true
}
const DebugConfigFile: string = "DebugConfig.json"
const DebugAccountFile: string = "DebugAccounts.json"

export let debugConfig: DebugConfig = releaseConfig;

/**
 * 通过最初的时候调用一次setDebugMode来初始化一次debugConfig一次.
 * @param b
 */
export function setDebugMode(b: boolean) {
  if (b) {
    if (FileManager.isRawFileExist(DebugConfigFile)) {
      let configJson = FileManager.readTextFromRawFileSync(DebugConfigFile);
      if (configJson) {
        let customConfig = JSON.parse(configJson) as DebugConfig
        logger.debug(`[read debug config] [success]`)
        debugConfig = customConfig;
      }
    }
  }
}

export interface DebugAccount {
  password: string,
  account: string
}

export function getDebugAccount(): DebugAccount | undefined {
  if (!FileManager.isRawFileExist(DebugAccountFile)) {
    return;
  }
  let json = FileManager.readTextFromRawFileSync(DebugAccountFile);
  if (!json) {
    return;
  }
  let debugAccounts = JSON.parse(json) as DebugAccount[];
  if (!debugAccounts.length) {
    return;
  }
  return debugAccounts[0];
}

